package com.course.controller;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.*;

import com.course.db.CourseBasic;
import com.course.db.OpenCourse;
import com.course.db.Teacher;
import com.et.mvc.JspView;

public class TeacherController  extends ApplicationController {
	
	//apply for opening a new course
	public JspView apply() throws Exception{
		

		   JspView view = new JspView("index.jsp");
	       System.out.println("apply");
	       
	       String strUserID = request.getSession().getAttribute("userID").toString();
	       
	       int teacher_id = 0;
	       Teacher teacher = new Teacher();
	       teacher.setUser_id(Integer.parseInt(strUserID));
	       teacher_id = teacher.getUser_id();
	       
//	       response.setContentType("text/xml; charset=UTF-8");
	       String teacher_name=request.getParameter("teacher_name");
	       String course_type=request.getParameter("course_type");
	       String course_name=request.getParameter("course_name");
	       String course_span=request.getParameter("course_span");
	       String credit=request.getParameter("credit");
	       String students_max=request.getParameter("students_max");
	       String students_min=request.getParameter("students_min");
	       String year=request.getParameter("year");
	       String [] terms=request.getParameterValues("term");
	       String [] grades=request.getParameterValues("grade");
	       String time=request.getParameter("time");
	       String location=request.getParameter("location");
	       String teach_style=request.getParameter("teach_style");
	       String test_style=request.getParameter("test_style");
	       String equipments=request.getParameter("equipments");
	       String introduction=request.getParameter("introduction");
	       
	       String course_term = "", course_grade = "";
	       
	       CourseBasic course = new CourseBasic();
	       course.setTeachers(teacher_name);
	       course.setType(course_type);
	       course.setName(course_name);
	       course.setCourse_span( Integer.parseInt(course_span) );
	       course.setCredit( Integer.parseInt(credit) );
	       course.setStudents_high( Integer.parseInt(students_max) );
	       course.setStudents_low( Integer.parseInt(students_min) );
	       course.setYear(year);
	       for(int i=0; i<terms.length; i++)
	       {
	    	   course_term += terms[i];
	    	   course_term += " ";
	       }
	       course.setOpenterm(course_term);
	       
	       for(int i=0; i<grades.length; i++)
	       {
	    	   course_grade += grades[i];
	    	   course_grade += " ";
	       }
	       course.setGrade(course_grade);
	       
	       course.setTeach_time(time);
	       course.setTeach_location(location);
	       course.setTeach_style(teach_style);
	       course.setTest_style(test_style);
	       course.setTeach_equipments(equipments);
	       course.setIntroduction(introduction);
	       
	       if(course.add())
	       {
	    	   System.out.println(" Add Course Ok!");
	    	   OpenCourse open_course = new OpenCourse();
	    	   open_course.setCheck_status(0);
	    	   open_course.setNum_choose_students(0);
	    	   open_course.setCourse_id(course.lastid());
	    	   open_course.setTeacher_id(teacher_id);
	    	   
	    	   if(open_course.add())
	    		   System.out.println(" Add opencourse Ok!");
	    	   else
	    		   System.out.println(" Add opencourse False!");
	    		   
	       }
	       else
	    	   System.out.println(" Add Course False!");
	       
	       response.sendRedirect("myCourse.do");
	       return null;
	}
	

	//check out my opening class
	public JspView myCourse() throws Exception{
		
		   JspView view = new JspView("teacher/mycourse.jsp");
	       System.out.println("mycourse");
	       
	       String strUserID = request.getSession().getAttribute("userID").toString();
	       
	       int teacher_id = 0;
	       Teacher teacher = new Teacher();
	       teacher.setUser_id(Integer.parseInt(strUserID));
	       teacher_id = teacher.getUser_id();
	       teacher.setId(teacher_id);
	       ResultSet courseRs = teacher.show_courses();
	       //request.getSession().setAttribute("myOpenCourse", courseRs);
	       
	       List< Hashtable<String, String> > courseList = new ArrayList< Hashtable<String, String> >();
			try{
				String value="";
				ResultSetMetaData courseRsMd = courseRs.getMetaData();
				int cols = courseRsMd.getColumnCount();
				while (courseRs.next())
				{
					Hashtable<String, String> hash = new Hashtable<String, String>();
					for (int i = 1; i <= cols; i++) {	
						String field = (String) (courseRsMd.getColumnName(i));
						int fieldtype = courseRsMd.getColumnType(i);
						if(fieldtype == java.sql.Types.VARCHAR )
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DOUBLE)
							value = (String)(Double.toString(courseRs.getDouble(i)));
						else if(fieldtype == java.sql.Types.INTEGER)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						
						if (value == null)
							value = "";
						hash.put(field, value);
					}
					courseList.add(hash);
				}
				
				request.setAttribute("CourseList", courseList);
				
			}catch(Exception e){
				e.printStackTrace();
			}
	       
	       return view;
	}
	public JspView coursedetail() throws Exception{
		
		JspView view = new JspView("search/coursedetail.jsp");
		System.out.println("coursedetail");
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());
		CourseBasic course =new CourseBasic();
		course.setId(courseId);
		course.init();
		request.setAttribute("Course", course);
		return view;
	}

}
